#include <bits/stdc++.h>

#define int long long

using namespace std;

void solve()
{
    int x;
    cin >> x;
    int mm = 1;
    int ans = 0;
    for(int i = 1; i <= 18; i ++)
    {
        mm *= 3;
    }
    for(int i = 18; i >= 0; i --)
    {
        while(x >= mm)
        {
            x -= mm;
            ans += mm * 3 + i * mm / 3;
        }
        mm /= 3;
    }
    cout << ans << endl;
}

signed main()
{
    int t;
    cin >> t;
    while(t --)
    {
        solve();
    }
}